package com.example.a2022application.activity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.example.a2022application.BaseActivity;
import com.example.a2022application.MainActivity;
import com.example.a2022application.MySqliteHelper;
import com.example.a2022application.R;
import com.example.a2022application.User;
import com.example.a2022application.bean.ClassesBean;
import com.example.a2022application.bean.RegisterBean;
import com.example.a2022application.net.Urls;
import com.example.a2022application.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

/**
 * 注册页面
 */
public class RegisterActivity extends BaseActivity {
    MySqliteHelper helper = null;
    private EditText etAccount; //账号
    private EditText etName; //名字
    private EditText etPassword; //密码
    private EditText etPasswordSure; //确认密码
    private TextView tvLogin; //返回登录
    private Button btnRegister; //注册按钮
    private Spinner spIdentity,spin_class;//身份
    private String getContent;
    private LinearLayout ll_class;

    private String classes = "";


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        helper = new MySqliteHelper(this);
        etAccount = (EditText) findViewById(R.id.et_account);//获取账号
        etName = (EditText) findViewById(R.id.et_nickName);//获取名字
        etPassword = (EditText) findViewById(R.id.et_password);//获取密码
        etPasswordSure = (EditText) findViewById(R.id.et_password_sure);//获取确认密码
        tvLogin = (TextView) findViewById(R.id.tv_login);//登录
        btnRegister = (Button) findViewById(R.id.btn_register);//获取注册按钮
        spIdentity = (Spinner) findViewById(R.id.spin_identity);//new Spinner
        ll_class = (LinearLayout) findViewById(R.id.ll_class);
        spin_class = (Spinner) findViewById(R.id.spin_class);
        tvLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到登录页面
                Intent intent=new Intent(RegisterActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        });
        spIdentity.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                getContent = RegisterActivity.this.getResources().getStringArray(R.array.professionals)[arg2];
                if ("学生".equals(getContent)){
                    ll_class.setVisibility(View.VISIBLE);
                    getClasses();
                }else {
                    ll_class.setVisibility(View.GONE);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        //设置注册点击按钮
        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //获取请求参数
                String account= etAccount.getText().toString();
                String nickName= etName.getText().toString();
                String password=etPassword.getText().toString();
                String passwordSure=etPasswordSure.getText().toString();
                Log.d("RegisterActivity", "OnCreate execute1");

                if ("".equals(account)){//账号不能为空
                    Toast.makeText(RegisterActivity.this,"账号不能为空", Toast.LENGTH_LONG).show();
                    return;
                }
                if ("".equals(nickName)){//昵称不能为空
                    Toast.makeText(RegisterActivity.this,"昵称不能为空", Toast.LENGTH_LONG).show();
                    return;
                }
                if ("".equals(password)){//密码为空
                    Toast.makeText(RegisterActivity.this,"密码为空", Toast.LENGTH_LONG).show();
                    return;
                }
                if (!password.equals(passwordSure)){//密码不一致
                    Toast.makeText(RegisterActivity.this,"密码不一致", Toast.LENGTH_LONG).show();
                    return;
                }
                if ("学生".equals(getContent)){
                    if (TextUtils.isEmpty(classes)){
                        ToastUtil.showToast("请选择班级");
                    }

                }
                OkGo.<String>post(Urls.REGISTER)
                        .params("account", account)
                        .params("nickname",nickName)
                        .params("spIdentity",getContent)
                        .params("password",password)
                        .params("classes",classes)
                        .execute(new StringCallback() {
                            @Override
                            public void onSuccess(Response<String> response) {
                                Log.e("TAG","注册---"+response.body());
                                try {
                                    RegisterBean bean = getGson().fromJson(response.body(), RegisterBean.class);
                                    if (bean.getCode()==0){
                                        startActivity(new Intent(RegisterActivity.this, MainActivity.class));
                                        finish();
                                        ToastUtil.showToast("注册成功");
                                    }else {
                                        ToastUtil.showToast(bean.getMessage());
                                    }

                                }catch (Exception e){

                                }
                            }
                        });

//                User mUser = null;
//                Log.d("RegisterActivity", "OnCreate execute3");
//
//                String sql = "select * from user where account = ? ";
//                SQLiteDatabase db = helper.getWritableDatabase();
//                Cursor cursor = db.rawQuery(sql, new String[]{account});
//                Log.d("RegisterActivity", "OnCreate execute4");
//
//                if (cursor != null && cursor.getColumnCount() > 0) {
//                    while (cursor.moveToNext()) {
//                        String dbId = cursor.getString(0);
//                        String dbAccount = cursor.getString(1);
//                        String dbName = cursor.getString(2);
//                        String dbPassword = cursor.getString(3);
//                        mUser = new User(dbId, dbAccount,dbName,dbPassword);
//                    }
//                }
//                if (mUser == null) {
//                    String insertSql = "insert into user(account,name,password) values(?,?,?)";
//                    db.execSQL(insertSql,new Object[]{account,nickName,password});
//                    Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
//                    finish();
//                }else {
//                    Toast.makeText(RegisterActivity.this, "该账号已存在", Toast.LENGTH_SHORT).show();
//                }
//                db.close();
            }
        });
    }

    private void getClasses() {
        OkGo.<String>post(Urls.SELECTCLASS)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        ClassesBean bean = getGson().fromJson(response.body(), ClassesBean.class);
                        if (bean.getCode() == 0){
                            List<String> strarr = new ArrayList<>();
                            for (int i = 0;i<bean.getData().size();i++){
                                strarr.add(bean.getData().get(i).getClass_name());
                            }
                            ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(RegisterActivity.this,
                                    android.R.layout.simple_spinner_item, strarr);
                            dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                            spin_class.setAdapter(dataAdapter);
                            spin_class.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                                @Override
                                public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                                    classes = strarr.get(i);
                                }

                                @Override
                                public void onNothingSelected(AdapterView<?> adapterView) {

                                }
                            });
                        }
                    }
                });
    }

    public void back(View view){
        finish();
    }
}
